﻿using MCPlatform.Dxf.Entities;
using netDxf;
using netDxf.Entities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Media;

namespace MCPlatform.Dxf.Utils
{
    public static class Utility
    {
        public static SolidColorBrush ConvertAciColorToBrush(AciColor aciColor)
        {

            // 假设AciColor有一个R, G, B属性，分别代表红、绿、蓝三种颜色的值  
            // 这里我们设置一个阈值来判断是否接近白色，比如每个分量的误差不超过10  
            const int whiteThreshold = 10; // 阈值，可以根据需要调整  

            // 检查颜色是否接近白色  
            bool isWhite = Math.Abs(aciColor.R - 255) <= whiteThreshold &&
                            Math.Abs(aciColor.G - 255) <= whiteThreshold &&
                            Math.Abs(aciColor.B - 255) <= whiteThreshold;

            // 根据检查结果创建并返回SolidColorBrush  
            if (isWhite)
            {
                // 如果是白色，返回黑色的SolidColorBrush  
                return new SolidColorBrush(Colors.Black); // 注意：这里使用了Colors.Black而不是Color.FromArgb  
            }
            else
            {
                // 否则，返回与AciColor对应的SolidColorBrush  
                Color color = Color.FromArgb(255, aciColor.R, aciColor.G, aciColor.B);
                return new SolidColorBrush(color);
            }
        }


        public static float? GetNumberFromTextToFloat(string lineWeigh, string layor)
        {
            string pattern = @"[-+]?\d+(\.\d+)?";

            // 创建正则表达式对象
            Regex regex = new Regex(pattern);

            // 在输入字符串中找到所有匹配项
            MatchCollection matches = regex.Matches(lineWeigh);

            MatchCollection matches2 = regex.Matches(layor);
            bool res1 = false;
            bool res2 = false;

            if (matches.Count > 0)
            {
                res1 = float.TryParse(matches[0].Value, out float result1);
                if (res1)
                {
                    return result1 / 100;
                }
            }

            if (matches2.Count > 0)
            {
                res2 = float.TryParse(matches2[0].Value, out float result2);
                if (res2)
                {
                    return result2 / 100;
                }
            }

            return null;


        }


        // 使用贪心算法对路径进行排序
        public static List<PathEntityBase> SortPathsGreedy(List<PathEntityBase> paths)
        {
            List<PathEntityBase> sortedPaths = new List<PathEntityBase>();

            // 如果路径列表为空，则返回空列表
            if (paths.Count == 0)
                return sortedPaths;

            // 将起始点设置为路径列表中的第一个路径段的起点

            // 找到最接近原点的起始点作为路径的起点
            PathEntityBase startPath = paths.OrderBy(p => p.StartPoint.X * p.StartPoint.X + p.StartPoint.Y * p.StartPoint.Y).First();
            Vector3 currentPoint = startPath.EndPoint;
            sortedPaths.Add(startPath);
            paths.Remove(startPath);

            // 循环，直到路径列表为空
            while (paths.Count > 0)
            {
                // 找到与当前点最近的路径段
                double minDistance = double.MaxValue;
                PathEntityBase nearestPath = new PathEntityBase();

                foreach (var path in paths)
                {
                    double distance = CalculateDistance(currentPoint, path.StartPoint);
                    if (distance < minDistance)
                    {
                        minDistance = distance;
                        nearestPath = path;
                    }
                }

                if (Math.Abs(currentPoint.X- nearestPath.StartPoint.X)>0.1 && Math.Abs(currentPoint.Y - nearestPath.StartPoint.Y) > 0.1)
                {
                    //增加直线过度点
                    PathEntityBase transitionPath = new PathEntityBase() { Type = new Line(), StartPoint = currentPoint, EndPoint = nearestPath.StartPoint };

                    sortedPaths.Add(transitionPath);
                }

               
                // 将最近的路径段添加到排序后的路径列表中
                sortedPaths.Add(nearestPath);
                paths.Remove(nearestPath);

                // 更新当前点为新添加的路径段的终点
                currentPoint = nearestPath.EndPoint;
            }

            return sortedPaths;
        }

        // 计算两个点之间的距离
        static double CalculateDistance(Vector3 point1, Vector3 point2)
        {
            double dx = point1.X - point2.X;
            double dy = point1.Y - point2.Y;
            double dz = point1.Z - point2.Z;

            return Math.Sqrt(dx * dx + dy * dy + dz * dz);
        }


        /// <summary>  
        /// 将netDxf库中的Vector3对象转换为double数组。  
        /// </summary>  
        /// <param name="vector">要转换的Vector3对象。</param>  
        /// <returns>包含Vector3的X, Y, Z分量的double数组。</returns>  
        public static double[] Vector3ToDoubleArray(Vector3 vector)
        {
            return new double[] { vector.X, vector.Y, vector.Z };
        }


        public static void SetEntityPunchPoint(Dictionary<Type, List<PathEntityBase>> dic, string handle, bool value)
        {
            foreach (var entityBases in dic.Values)
            {
                foreach (var enti in entityBases)
                {
                    if (handle == enti.Handle)
                    {
                        enti.IsPunchPoint = value;
                    }
                }


            }
        }

        public static void SetEntityVisionPoint(Dictionary<Type, List<PathEntityBase>> dic, string handle, bool value)
        {
            foreach (var entityBases in dic.Values)
            {
                foreach (var enti in entityBases)
                {
                    if (handle == enti.Handle)
                    {
                        enti.IsVisionPoint = value;
                    }
                }


            }
        }


        public static void SetEntityCuttingPoint(Dictionary<Type, List<PathEntityBase>> dic, string handle, bool value)
        {
            foreach (var entityBases in dic.Values)
            {
                foreach (var enti in entityBases)
                {
                    if (handle == enti.Handle)
                    {
                        enti.IsCuttingPoint = value;
                    }
                }


            }
        }

    }
}
